package notify

import (
	//"iot-base/common/cache"
	"time"
)

type OnlineNotify struct {
	TID       string
	TimeStamp time.Time
}

func sendMsg(tid string, timestampl time.Time, mtype string) {
	pMsg := &OnlineNotify{
		TID:       tid,
		TimeStamp: timestampl,
	}
	PubMsg(false, mtype, pMsg)
}
func subOnlineMsg(handle func(pMsg *OnlineNotify), stype string) {
	SubMsg(false, stype, &OnlineNotify{}, func(t string, body interface{}) {
		pOnline, ok := body.(*OnlineNotify)
		if ok {

			if handle != nil {
				handle(pOnline)
			}
		}

	})
}

//PubOnline 发布在线消息
func PubOnline(tid string, timestampl time.Time) {
	sendMsg(tid, timestampl, "online")
}

//SubOnline 订阅设备在线消息
func SubOnline(handle func(pMsg *OnlineNotify)) {
	subOnlineMsg(handle, "online")
}

//PubOffline 发布离线消息
func PubOffline(tid string, timestampl time.Time) {
	sendMsg(tid, timestampl, "offline")
}

//SubOffline 订阅设备离线消息
func SubOffline(handle func(pMsg *OnlineNotify)) {
	subOnlineMsg(handle, "offline")
}
